﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pixysoft
{
    public class FormatStringBuilder
    {
        StringBuilder builder = new StringBuilder();

        int indent = 0;

        int blankCount = 4;

        public FormatStringBuilder() : this(4) { }

        public FormatStringBuilder(int blankCount)
        {
            this.blankCount = blankCount;
        }

        public void Indent()
        {
            indent++;
        }

        public void Outdent()
        {
            indent--;
        }

        public void AppendIndent()
        {
            for (int i = 0; i < indent; i++)
            {
                for (int j = 0; j < blankCount; j++)
                    builder.Append(" ");
            }
        }

        /// <summary>
        /// no indent, no return
        /// </summary>
        /// <param name="format"></param>
        /// <param name="paras"></param>
        public void Append(string format, params object[] paras)
        {
            AppendContent(string.Format(format, paras));
        }

        /// <summary>
        /// no indent, no return
        /// </summary>
        /// <param name="content"></param>
        public void Append(string content)
        {
            AppendContent(content);
        }

        /// <summary>
        /// newline with indent / return
        /// </summary>
        /// <param name="format"></param>
        /// <param name="paras"></param>
        public void AppendLine(string format, params object[] paras)
        {
            AppendIndent();
            AppendContent(string.Format(format, paras));
            AppendLine();
        }

        /// <summary>
        /// newline with indent / return
        /// </summary>
        /// <param name="content"></param>
        public void AppendLine(string content)
        {
            AppendIndent();
            AppendContent(content);
            AppendLine();
        }

        /// <summary>
        /// append empty line
        /// </summary>
        public void AppendLine()
        {
            builder.AppendLine();
        }

        public void Remove(int index, int length)
        {
            builder.Remove(index, length);
        }

        public int Length
        {
            get { return builder.Length; }
        }


        public override string ToString()
        {
            return builder.ToString();
        }

        private void AppendContent(string content)
        {
            builder.Append(content);
        }

    }
}
